package com.xtravel.discovery.viewholder;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.lidroid.xutils.BitmapUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import com.xtravel.R;
import com.xtravel.common.helper.ShareSdkHelper;
import com.xtravel.common.widget.RoundImageView;
import com.xtravel.common.widget.TweetImageView;
import com.xtravel.discovery.activity.BrowserActivity;
import com.xtravel.discovery.activity.LiveDetailActivity;
import com.xtravel.discovery.api.LiveRequest;
import com.xtravel.discovery.dto.LiveRecord;
import com.xtravel.discovery.widget.LiveReplayMarkView;

/**
 * Created by memory on 15/12/5.
 */

public class LiveRecordsViewHolder extends BaseViewHolder<LiveRecord>{
    private LiveRequest apiRequest = new LiveRequest();
    private TextView mainTitle;
    private TextView liveSubTitle;
    private TextView likeCount;
    private ImageButton likeImg;
    private TextView favoriteCount;
    private ImageButton favoriteImg;
    private TextView shareCount;
    private ImageButton shareImg;
    private TextView viewCount;
    private TextView liveName;
    private TextView liveAuthorName;
    private RoundImageView authorImg;
    private RelativeLayout liveImagePanel, liveInfoPanel;

    private TweetImageView liveCover;
    private LiveReplayMarkView liveReplayMarkView;
    private View likePanel, sharePanel, fravoritePanel;
    private int imgWidth;

    public LiveRecordsViewHolder(Context context, BitmapUtils bitmapUtils, LayoutInflater layoutInflater, int imgWidth)
    {
        super(context, bitmapUtils, layoutInflater);
        this.imgWidth = imgWidth;
    }

    @Override
    public View getView() {
        final View convertView = layoutInflater.inflate(R.layout.live_records_list_item_single, null);
        this.mainTitle = (TextView) convertView.findViewById(R.id.live_main_title_single);
        this.liveSubTitle = (TextView) convertView.findViewById(R.id.live_sub_title);
        this.liveCover = (TweetImageView) convertView.findViewById(R.id.country_live_post);
        this.liveReplayMarkView = (LiveReplayMarkView)convertView.findViewById(R.id.live_mark_view);
        this.likeCount = (TextView) convertView.findViewById(R.id.live_like_count);
        this.likeImg = (ImageButton) convertView.findViewById(R.id.live_like_img);
        this.favoriteCount = (TextView) convertView.findViewById(R.id.live_favorite_count);
        this.favoriteImg = (ImageButton) convertView.findViewById(R.id.live_favorite_img);
        this.shareCount = (TextView) convertView.findViewById(R.id.live_share_count);
        this.shareImg = (ImageButton) convertView.findViewById(R.id.live_share_img);
        this.viewCount = (TextView) convertView.findViewById(R.id.live_view_count);
        this.liveName = (TextView) convertView.findViewById(R.id.live_name);
        this.liveAuthorName = (TextView) convertView.findViewById(R.id.live_author);
        this.authorImg = (RoundImageView) convertView.findViewById(R.id.live_author_img);
        this.liveImagePanel = (RelativeLayout) convertView.findViewById(R.id.live_img_panel);
        this.liveInfoPanel = (RelativeLayout) convertView.findViewById(R.id.live_info_panel);
        this.liveImagePanel.setOnClickListener(liveItemClick);
        this.liveInfoPanel.setOnClickListener(liveItemClick);
        this.liveCover.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, imgWidth));
        likePanel= convertView.findViewById(R.id.like_panel);
        likePanel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final LiveRecord record = (LiveRecord) view.getTag();
                new AsyncTask<Long, Void, Boolean>() {
                    @Override
                    protected Boolean doInBackground(Long... params) {
                        return apiRequest.like(Long.valueOf(record.id), !record.isLiked);
                    }

                    @Override
                    protected void onPostExecute(Boolean isOk) {
                        super.onPostExecute(isOk);
                        if (isOk) {
                            record.isLiked = !record.isLiked;
                            record.likedCount += (record.isLiked ? 1 : -1);
                            likeCount.setText(record.likedCount + "");
                            likeImg.setEnabled(record.isLiked);
                        }
                    }
                }.execute();
            }
        });

        sharePanel= convertView.findViewById(R.id.share_panel);
        sharePanel.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                final LiveRecord record = (LiveRecord)view.getTag();
                ArrayList<String> covers = new ArrayList<String>();
                covers.add(record.cover);
                ShareSdkHelper.share(context, "分享", record.name, record.liveUrl, covers);
                new AsyncTask<Long, Void, Boolean>() {
                    @Override
                    protected Boolean doInBackground(Long... params) {
                        return apiRequest.share(Long.valueOf(record.id));
                    }

                    @Override
                    protected void onPostExecute(Boolean isOk) {
                        super.onPostExecute(isOk);
                        if(isOk)
                        {
                            record.retweetCount++;
                            shareCount.setText(record.retweetCount + "");
                            shareImg.setEnabled(true);
                        }
                    }
                }.execute();
            }
        });

        fravoritePanel= convertView.findViewById(R.id.favorite_panel);
        fravoritePanel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final LiveRecord record = (LiveRecord)view.getTag();
                new AsyncTask<Long, Void, Boolean>() {
                    @Override
                    protected Boolean doInBackground(Long... params) {
                        return apiRequest.favorite(Long.valueOf(record.id), !record.isFavorted);
                    }

                    @Override
                    protected void onPostExecute(Boolean isOk) {
                        super.onPostExecute(isOk);
                        if(isOk)
                        {
                            record.isFavorted = !record.isFavorted;
                            record.favoriteCount += (record.isFavorted ? 1 : -1);
                            favoriteCount.setText(record.favoriteCount + "");
                            favoriteImg.setEnabled(record.isFavorted);
                        }
                    }
                }.execute();
            }
        });
        convertView.setTag(this);
        return convertView;
    }

    private View.OnClickListener liveItemClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            LiveRecord live = (LiveRecord)view.getTag();
            if(live == null)
                return;

            //Intent it = new Intent(Intent.ACTION_VIEW, Uri.parse("http://121.41.88.34/player.html"));
            //it.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
            Intent intent = new Intent(context, BrowserActivity.class);
            intent.putExtra("url", "http://121.41.88.34/player.html");
            context.startActivity(intent);
//
//            Intent intent = new Intent(context, LiveDetailActivity.class);
//            intent.putExtra("id", live.id);
//            intent.putExtra("liveName", live.name);
//            intent.putExtra("liveCover", live.cover);
//            intent.putExtra("url", live.liveUrl);
//            intent.putExtra("background", live.cover);
//            intent.putExtra("startTime", live.createdTime);
//            intent.putExtra("duration", live.duration);
//            intent.putExtra("userId", live.anchorId);
//            intent.putExtra("userName", live.anchor.nickname);
//            intent.putExtra("userHP", live.anchor.avatarUri);
//            intent.putExtra("location", live.location.address);
//            intent.putExtra("likeCount", live.likedCount);
//            intent.putExtra("imGroupId", live.groupId);
//            intent.putExtra("isLive", live.isLive);
//            context.startActivity(intent);
        }
    };

    @Override
    public void setValue(LiveRecord record) {
        this.mainTitle.setText(record.countryName);
        if(record.location != null)
            this.liveSubTitle.setText(record.location.cityName);
        this.likeCount.setText(record.likedCount + "");
        this.likePanel.setTag(record);
        this.favoriteCount.setText(record.favoriteCount + "");
        this.fravoritePanel.setTag(record);
        this.shareCount.setText(record.retweetCount + "");
        this.sharePanel.setTag(record);
        this.viewCount.setText(record.viewerCount + "");
        this.liveName.setText(record.name);
        this.liveCover.setImageUrl(this.bitmapUtils, record.cover);
        if(record.anchor != null) {
            this.liveAuthorName.setText(record.anchor.nickname);
            if (StringUtils.isNotBlank(record.anchor.avatarUri))
                this.authorImg.setImageUrl(bitmapUtils, record.anchor.avatarUri);
            else
                this.authorImg.setImageUrl(bitmapUtils, "http://image.uisdc.com/wp-content/uploads/2015/10/Travel-Coffee-Book2015-10-04_1125.png");
        }

        this.liveReplayMarkView.setText(record.isLive, record.duration);
        if(record.isLive) {
            this.liveReplayMarkView.setVisibility(View.GONE);
        }

        favoriteImg.setEnabled(record.isFavorted);
        likeImg.setEnabled(record.isLiked);
        liveImagePanel.setTag(record);
        liveInfoPanel.setTag(record);
    }
}
